//import {Component} from '../ZRF';
import React,{ZRComponent} from '../ZRF';
import Topic from '../../common/data/Topic';
import {BSTree,RBTree,AVLTree,Range} from '../../common/mdb';
let t=new RBTree();

function pad(x,n){
    if (x<10) return " ".repeat(n-1)+x;
    if (x<100) return " ".repeat(n-2)+x;
    if (x<1000) return " ".repeat(n-3)+x;
    return x+"";
}

function printTree() {
    let i = 0;
    t.visit((node, depth)=> {
        console.log(["[", pad(i, 3), "] ", pad(depth, 3), " ", "...|".repeat(depth), node.value, " ", node.color, " ", node.depth].join(""));
        ++i;
    });
}

function insert(arr){
    for (let e of arr) {
        console.log("insert",e,t);
        t.insert(e);
        t.assert();
    }
}

function remove(arr){
    for (let e of arr){
        console.log("remove",e);
        t.remove(e);
        //printTree();
        t.assert();
    }
}

function randomArr(len,min,max){
    let arr=[];
    while(len-->0) arr.push(min+Math.floor(Math.random()*max));
    console.log(arr);
    return arr;
}

export  default class Test extends ZRComponent{
    render(){
        for (let i=0;i<100;++i) {
            console.clear();
            t=new RBTree();
            insert(randomArr(129, 0, 100));
            //insert([1, 7, 2, 8, 9, 2, 2, 6, 6, 3]);
            printTree(t);
            remove(randomArr(129, 0, 100));
            //remove([6, 5, 6, 3, 5, 9, 0, 7, 4, 6]);
        }
        //
        //t.assert();
        let range=new Range({
            min:10,
            max:20
        },t.comp);
        let out=[];
        t.visit(v=>{if (range.inRange(v.value)) out.push(v.value);});
        console.log(out);
        console.log(t.query(range).map(e=>e.value));
        return <div>test</div>
    }
}